package com.hz.basic.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hz.basic.entity.*;
import com.hz.basic.service.*;
import com.hz.common.utils.PageUtils;
import com.hz.common.utils.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("stiffchinesebrush")
@Api(tags="硬笔")
public class StiffChineseBrushController {

    @Autowired
    private CbStiffSystemService stiffSystemService;

    @Autowired
    private CbStiffBookService stiffBookService;

    @Autowired
    private CbStiffUnitService stiffUnitService;

    @Autowired
    private CbStiffChapterService stiffChapterService;

    @Autowired
    private CbStiffChapterPictureService stiffChapterPictureService;

    @Autowired
    private CbStiffTypefaceService stiffTypefaceService;

    @Autowired
    private CbStiffReadingService stiffReadingService;

    @Autowired
    private CbStiffQuickspotService stiffQuickspotService;

    @Autowired
    private CbStiffCalligraphyService stiffCalligraphyService;

    @Autowired
    private CbStiffCalligraphyPictureService stiffCalligraphyPictureService;

    @Autowired
    StringRedisTemplate redisTemplate;

    @Autowired
    private CbExpandEditionService cbExpandEditionService;

    @Autowired
    private CbExpandResourceClassificationService cbERClassificationService;

    @Autowired
    private CbExpandResourceService cbExpandResourceService;

    @ApiOperation("获取六步按钮")
    @GetMapping("/getstiffbutton")
    @ApiImplicitParam(name = "tid", value = "生字id", paramType = "query", dataType = "Long", required = true)
    public ResponseData<List<String>> getstiffbutton(@RequestParam("tid") Long tid){
        List<String> buttonList = new ArrayList<>();
        List<CbStiffReadingEntity> readings = stiffReadingService.list(new QueryWrapper<CbStiffReadingEntity>().eq("tid", tid));
        if (readings.size() > 0)    buttonList.add("认读");
        CbStiffTypefaceEntity typeface = stiffTypefaceService.getById(tid);
        if(typeface != null) {
            if (typeface.getChalkVideo() != null && !typeface.getChalkVideo().equals("") && typeface.getChalkVideoDescription() != null && !typeface.getChalkVideoDescription().equals(""))
                buttonList.add("粉笔视频");
            if (typeface.getPenVideo() != null && !typeface.getPenVideo().equals("") && typeface.getPenVideoDescription() != null && !typeface.getPenVideoDescription().equals(""))
                buttonList.add("钢笔视频");
            List<CbStiffQuickspotEntity> quickspots = stiffQuickspotService.list(new QueryWrapper<CbStiffQuickspotEntity>().eq("tid", tid));
            if (quickspots != null && quickspots.size() == 4) buttonList.add("一起来找茬");
            if (typeface.getLiteracyVideo() != null && !typeface.getLiteracyVideo().equals("")) buttonList.add("识字视频");
            List<CbStiffCalligraphyEntity> stiffCalligraphys = stiffCalligraphyService.list(new QueryWrapper<CbStiffCalligraphyEntity>().eq("tid", tid));
            if (stiffCalligraphys != null) buttonList.add("书法基础");
        }
        return ResponseData.success(buttonList);
    }

    @ApiOperation("硬笔书扩展资源分类")
    @GetMapping("/getstiffbookeditionclassification")
    @ApiImplicitParam(name = "eid", value = "扩展版本id", paramType = "query", dataType = "Long", required = true)
    public ResponseData<List<CbExpandEditionResourceEntity>> getstiffbookeditionclassification(@RequestParam("eid") Long eid){
        List<CbExpandResourceClassificationEntity> expandEditionResourceClassifications = cbERClassificationService.list(new QueryWrapper<CbExpandResourceClassificationEntity>().eq("eid",eid).orderByAsc("corder"));
        return ResponseData.success(expandEditionResourceClassifications);
    }

    @ApiOperation("硬笔书扩展版本资源")
    @GetMapping("/getexpandresource")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cid", value = "分类id，012分别查询（0：碑帖欣赏1：国学经典2：书法知识）所有资源", paramType = "query", dataType = "Long", required = true),
            @ApiImplicitParam(name = "condition", value = "查询条件", paramType = "query", dataType = "String", required = false),
            @ApiImplicitParam(name = "curpage", value = "分页，当前页", paramType = "query", dataType = "int", required = true),
            @ApiImplicitParam(name = "numpage", value = "分页，页数量", paramType = "query", dataType = "int", required = true)
    })
    public ResponseData<PageUtils<List<CbExpandResourceEntity>>> getexpandresource(@RequestParam("cid") Long cid,@RequestParam(name = "condition",required = false) String condition
            ,@RequestParam(name = "curpage") int curpage,@RequestParam(name = "numpage") int numpage){
        PageUtils<List<CbExpandResourceEntity>> page;
        if (cid == 0 || cid == 1 || cid == 2)   page = cbExpandResourceService.getallstiffexpandresource(cid,condition,curpage,numpage);
        else page = cbExpandResourceService.getexpandresource(cid,condition,curpage,numpage);
        return ResponseData.success(page);
    }

    @ApiOperation("硬笔教学系统")
    @GetMapping("/getstiffsystem")
    @ApiImplicitParam(name = "Authorization",value = "token",paramType = "header",dataType = "String",required = true)
    public ResponseData<List<CbStiffSystemEntity>> getstiffsystem(@RequestHeader("Authorization") String token){
        long userId = Long.parseLong(redisTemplate.opsForValue().get("user:" + token));
        List<CbStiffSystemEntity> stiffSystems = stiffSystemService.getstiffsystem(userId);
        List<CbStiffSystemEntity> stiffBookExpandEditions = cbExpandEditionService.getstiffbookexpandedition(userId);
        for (CbStiffSystemEntity stiffSystemEntity : stiffBookExpandEditions)
            stiffSystems.add(stiffSystemEntity);
        Collections.sort(stiffSystems, Comparator.comparing(CbStiffSystemEntity::getOrder));
        return ResponseData.success(stiffSystems);
    }

    @ApiOperation("硬笔书")
    @GetMapping("/getstiffbook")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "sid", value = "硬笔教学系统id", paramType = "query", dataType = "Long", required = true),
        @ApiImplicitParam(name = "Authorization",value = "token",paramType = "header",dataType = "String",required = true)
    })
    public ResponseData<List<CbStiffBookEntity>> getstiffbook(@RequestParam("sid") Long sid, @RequestHeader("Authorization") String token){
        long userId = Long.parseLong(redisTemplate.opsForValue().get("user:" + token));
        List<CbStiffBookEntity> stiffBooks = stiffBookService.getstiffbook(userId,sid);
        return ResponseData.success(stiffBooks);
    }

    @ApiOperation("获取目录")
    @GetMapping("/getcontentsbybid")
    @ApiImplicitParam(name = "bid", value = "硬笔书id", paramType = "query", dataType = "Long", required = true)
    public ResponseData<List<CbStiffUnitEntity>> getcontentsbybid(@RequestParam("bid") Long bid){
        List<CbStiffUnitEntity> units = stiffUnitService.list(new QueryWrapper<CbStiffUnitEntity>().eq("bid", bid).orderByAsc("uorder"));
        for (CbStiffUnitEntity unit:units) {
            List<CbStiffChapterEntity> chapters = stiffChapterService.list(new QueryWrapper<CbStiffChapterEntity>().eq("uid",unit.getId()).orderByAsc("corder"));
            unit.setChapters(chapters);
        }
        return ResponseData.success(units);
    }

    @ApiOperation("每章节内容")
    @GetMapping("/getchappicbycid")
    @ApiImplicitParam(name = "cid", value = "章节id", paramType = "query", dataType = "Long", required = true)
    public ResponseData<List<CbStiffChapterPictureEntity>> getchappicbycid(@RequestParam("cid") Long cid){
        List<CbStiffChapterPictureEntity> chapterPictures = stiffChapterPictureService.list(new QueryWrapper<CbStiffChapterPictureEntity>().eq("cid", cid).orderByAsc("corder"));
        return ResponseData.success(chapterPictures);
    }

    @ApiOperation("获取生字学习")
    @GetMapping("/gettypefacebycid")
    @ApiImplicitParam(name = "cid", value = "章节id", paramType = "query", dataType = "Long", required = true)
    public ResponseData<List<CbStiffTypefaceEntity>> gettypefacebycid(@RequestParam("cid") Long cid){
        List<CbStiffTypefaceEntity> typefaces = stiffTypefaceService.list(new QueryWrapper<CbStiffTypefaceEntity>().eq("cid", cid).orderByAsc("torder"));
        return ResponseData.success(typefaces);
    }

    @ApiOperation("认读")
    @GetMapping("/getreadingbytid")
    @ApiImplicitParam(name = "tid", value = "生字id", paramType = "query", dataType = "Long", required = true)
    public ResponseData<CbStiffReadingEntity> getreadingbytid(@RequestParam("tid") Long tid){
        CbStiffReadingEntity reading = stiffReadingService.getOne(new QueryWrapper<CbStiffReadingEntity>().eq("tid", tid).last("limit 1"));
        return ResponseData.success(reading);
    }

    @ApiOperation("找茬")
    @GetMapping("/getquickspotbytid")
    @ApiImplicitParam(name = "tid", value = "生字id", paramType = "query", dataType = "Long", required = true)
    public ResponseData<List<CbStiffQuickspotEntity>> getquickspotbytid(@RequestParam("tid") Long tid){
        List<CbStiffQuickspotEntity> quickspots = stiffQuickspotService.list(new QueryWrapper<CbStiffQuickspotEntity>().eq("tid", tid));
        return ResponseData.success(quickspots);
    }

    @ApiOperation("书法基础")
    @GetMapping("/getcalligraphybytid")
    @ApiImplicitParam(name = "type", value = "1基本笔画2偏旁部首3结构形式", paramType = "query", dataType = "int", required = true)
    public ResponseData<List<CbStiffCalligraphyEntity>> getcalligraphybytid( @RequestParam("type") int type){
        List<CbStiffCalligraphyEntity> stiffCalligraphys = stiffCalligraphyService.list(new QueryWrapper<CbStiffCalligraphyEntity>().eq("type", type));
        for (CbStiffCalligraphyEntity stiffCalligraphy : stiffCalligraphys){
            List<CbStiffCalligraphyPictureEntity> list = null;
            if (stiffCalligraphys != null)
                list = stiffCalligraphyPictureService.list(new QueryWrapper<CbStiffCalligraphyPictureEntity>().eq("cid", stiffCalligraphy.getId()).orderByAsc("porder"));
            stiffCalligraphy.setCalligraphyPictures(list);
        }
        return ResponseData.success(stiffCalligraphys);
    }

    @ApiOperation("粉笔视频")
    @GetMapping("/getchalkvideobycid")
    @ApiImplicitParam(name = "tid", value = "生字id", paramType = "query", dataType = "Long", required = true)
    public ResponseData<CbStiffChalkVideoEntity> getchalkvideobycid(@RequestParam("tid") Long tid){
        CbStiffChalkVideoEntity chalkVideo = stiffTypefaceService.getchalkvideobytid(tid);
        return ResponseData.success(chalkVideo);
    }

    @ApiOperation("钢笔视频")
    @GetMapping("/getpenvideobytid")
    @ApiImplicitParam(name = "tid", value = "生字id", paramType = "query", dataType = "Long", required = true)
    public ResponseData<CbStiffPenVideoEntity> getpenvideobytid(@RequestParam("tid") Long tid){
        CbStiffPenVideoEntity penVideo = stiffTypefaceService.getpenvideobytid(tid);
        return ResponseData.success(penVideo);
    }

    @ApiOperation("识字视频")
    @GetMapping("/getliteracyvideobytid")
    @ApiImplicitParam(name = "tid", value = "生字id", paramType = "query", dataType = "Long", required = true)
    public ResponseData<CbStiffLiteracyVideoEntity> getliteracyvideobytid(@RequestParam("tid") Long tid){
        CbStiffLiteracyVideoEntity literacyVideo = stiffTypefaceService.getliteracyvideobytid(tid);
        return ResponseData.success(literacyVideo);
    }

    @ApiOperation("硬笔书按条件检索字")
    @GetMapping("/getstifftypefacesbycondition")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "content", value = "搜索框内容", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "curpage", value = "分页，当前页", paramType = "query", dataType = "int", required = true),
            @ApiImplicitParam(name = "numpage", value = "分页，页数量", paramType = "query", dataType = "int", required = true),
            @ApiImplicitParam(name = "Authorization",value = "token",paramType = "header",dataType = "String",required = true)
    })
    public ResponseData<PageUtils<List<CbStiffTypefaceEntity>>> getstifftypefacesbycondition(@RequestParam("content") String content,
                                                                                                @RequestParam(name = "curpage") int curpage,
                                                                                                @RequestParam(name = "numpage") int numpage,
                                                                                                @RequestHeader("Authorization") String token){
        long userId = Long.parseLong(redisTemplate.opsForValue().get("user:" + token));
        PageUtils<List<CbStiffTypefaceEntity>> page =  stiffTypefaceService.getstifftypefacesbycondition(content, curpage, numpage);
        return ResponseData.success(page);
    }
}
